package com.linln.admin.system.controller;

import com.linln.admin.system.service.AppService;
import com.linln.admin.system.validator.DictValid;
import com.linln.common.constant.StatusConst;
import com.linln.common.enums.ResultEnum;
import com.linln.common.enums.StatusEnum;
import com.linln.common.exception.ResultException;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.utils.ValidationUtil;
import com.linln.common.vo.ResultVo;
import com.linln.component.actionLog.action.SaveAction;
import com.linln.component.actionLog.action.StatusAction;
import com.linln.component.actionLog.annotation.ActionLog;
import com.linln.component.actionLog.annotation.EntityParam;
import com.linln.component.shiro.ShiroUtil;
import com.linln.component.thymeleaf.utility.DictUtil;
import com.linln.modules.system.domain.Dict;
import com.linln.modules.system.domain.Menu;
import com.linln.modules.system.domain.Role;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.repository.DictRepository;
import com.linln.modules.system.repository.UserRepository;
import com.linln.modules.system.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author 小懒虫
 * @date 2018/8/14
 */
@Controller
@RequestMapping("/system/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private AppService appService;

    @Autowired
    private DictRepository dictRepository;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("system:dict:index")
    public ResponseEntity<?> index(Model model, Dict dict){

        return appService.layTable(dictRepository, before -> {

                    // 添加模板变量
                    return model;

                },

                after -> {

                    // 添加查询条件
                    return (Specification<User>) (root, query, cb) -> {
                        Predicate predicate = cb.conjunction();  // 初始条件
//                        predicate = cb.and(predicate, cb.equal(root.get("username"), "admin"));
//                        predicate = cb.and(predicate, cb.greaterThan(root.get("age"), age));
//                        predicate = cb.and(predicate, cb.equal(root.get("email"), email));
                        return predicate;
                    };
                },

                "/system/dict/index",

                (pageResult) -> {
                    // 在这里处理 pageResult
                    // 例如：修改每一条数据
                    pageResult.getContent().forEach(item -> {
                        // 对每一项数据进行处理
                        // (User) item;
                    });
                }

        );


    }


    /**
     * 跳转到添加页面
     */
    @RequestMapping("/form")
    @RequiresPermissions("system:dict:form")
    public ResponseEntity<?> form(Model model, @EntityParam Dict dict) throws Exception {
        return appService.form(dictRepository, "/system/dict/form", (isEdit) -> {

            return model;

        }, (data) -> {

            return ValidationUtil.validateFields(new ArrayList<>());

        }, (data) -> {
            // 数据处理
            return dict;
        },
        (data) -> {
            // 保存后续逻辑在这写
        });
    }

    /**
     * 删除菜单
     */
    @RequestMapping("/remove")
    @RequiresPermissions("system:dict:remove")
    public ResponseEntity<?> remove(@RequestParam(value = "id") Dict dict)
    {
        return appService.save(dictRepository, (data) -> {
            // 校验数据
            return ValidationUtil.validateFields(new ArrayList<>());
        }, (data) -> {
            dict.setStatus(StatusConst.DELETE);
            return dict;
        }, (data) -> {
            // 更新后操作
        });

    }

}
